home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 11 Learning / 01 Manslow / GPExample / CGPANDNode.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-10  |  2.5 KB  |  108 lines

  1. //GPExample
  2. //Copyright John Manslow
  3. //29/09/2001
  4.  
  5. ////////////////////////////////////////////////////////////
  6. //Only when comoiling under Windows
  7. #include "stdafx.h"
  8. #define new DEBUG_NEW
  9. ////////////////////////////////////////////////////////////
  10.  
  11. #include "CGPANDNode.h"
  12. #include "CGPNode.h"
  13. #include "CGP.h"
  14. #include "assert.h"
  15.  
  16. extern CGPNode *pGPCreateRandomSubtree(void);
  17.  
  18. extern CGPNode **pPrototypeList;
  19. extern unsigned long ulNumberOfPrototypes;
  20.  
  21. //All real classes (that is, noes that can actually form part of a program) have an automatic declaration
  22. //like this, which causes the prortype list to be created and the class to be entered into it
  23. CGPANDNode ANDPrototype;
  24.  
  25. CGPANDNode::CGPANDNode()
  26. {
  27.     unsigned long i;
  28.  
  29.     //Create the class name
  30.     sprintf(psName,"ANDNode");
  31.  
  32.     //Define its type ID
  33.     nType=100;
  34.  
  35.     //Set the children pointer to NULL so we know its uninitialsied
  36.     pChildren=NULL;
  37.  
  38.     //AND has two arguments (and hence children)
  39.     ulNumberOfChildren=2;
  40.  
  41.     //Try to add the node to the prortype list (and in so doing find out whether it is a prototype)
  42.     nIsPrototype=1;
  43.     if(AddToPrototypeList())
  44.     {
  45.         nIsPrototype=0;
  46.         pChildren=new CGPNode*[ulNumberOfChildren];
  47.         for(i=0;i<ulNumberOfChildren;i++)
  48.         {
  49.             pChildren[i]=NULL;
  50.         }
  51.     }
  52.     dPrior=0.1;
  53. }
  54.  
  55. CGPANDNode::~CGPANDNode()
  56. {
  57. }
  58.  
  59. CGPNode *CGPANDNode::pGetCopy(CGP* pGP)
  60. {
  61.     unsigned long i;
  62.  
  63.     CGPANDNode *pNewNode=new CGPANDNode;
  64.     assert(!(pNewNode==NULL));
  65.  
  66.     for(i=0;i<ulNumberOfChildren;i++)
  67.     {
  68.         if(pNewNode->pChildren[i]!=NULL)
  69.         {
  70.             delete pNewNode->pChildren[i];
  71.         }
  72.  
  73.         if(!nIsPrototype)
  74.         {
  75.             pNewNode->pChildren[i]=pChildren[i]->pGetCopy(pGP);
  76.         }
  77.         else
  78.         {
  79.             pNewNode->pChildren[i]=pGP->pGetRandomSubtree();
  80.         }
  81.     }
  82.     return (CGPNode*)pNewNode;
  83. }
  84.  
  85. double CGPANDNode::dEvaluate(void)
  86. {
  87.     //Return the arithmetical-logical  AND of the return values of the node's children
  88.     return pChildren[0]->dEvaluate()*pChildren[1]->dEvaluate();
  89. }
  90.  
  91. char* CGPANDNode::psGetString(char* pString)
  92. {
  93.     //Append this node's function and that of its associated subtree to the string passed into the funciton
  94.     char *pSubString=new char[50000];
  95.     if(pString!=NULL && strlen(pString)<10000)
  96.     {
  97.         sprintf(pSubString,"");
  98.         pSubString=pChildren[0]->psGetString(pSubString);
  99.         sprintf(pString,"%s AND((%s),",pString,pSubString);
  100.         sprintf(pSubString,"");
  101.         pSubString=pChildren[1]->psGetString(pSubString);
  102.         sprintf(pString,"%s(%s))",pString,pSubString);
  103.     }
  104.     delete []pSubString;
  105.     return pString;
  106. }
  107.  
  108.